let token = sessionStorage.getItem("token");
$(function () {
    $("#name").html(sessionStorage.getItem("username"));


    $('#datetimepicker1').datetimepicker({
        format: 'yyyy-mm-dd hh:ii:ss'
    });
    $('#datetimepicker2').datetimepicker({
        format: 'yyyy-mm-dd hh:ii:ss'
    });

    // 添加会议信息
    $("#go").click(function () {
        let type = $("[name='type']:checked").val();
        let name = $("#meetingName").val();
        let jointlyers = $("#joinId").val();
        let start_time = $(".start_time").val();
        let end_time = $(".end_time").val();
        let address = $("#address").val();
        let taker = $("#taker").val();
        let remark = $("#remark").val();
        let str = {"name":name,"type":type,"joiners":jointlyers,"start_time":start_time,"end_time":end_time,"address":address,"taker":taker,"remark":remark};
        console.log(str);
        $.ajax({
            url:testurl + "meeting/",
            headers: { "Authorization": "Bearer " + token},
            type: "post",
            contentType: "application/json",
            data:JSON.stringify(str),
            success: function (data) {

                if (data.success === true) {
                    $(".users").html("添加会议");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#my-popup').modal('close');
                    loadData(1);
                }else {
                    $(".users").html("添加会议");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#my-popup').modal('close');
                }
            }
        });
        return false
    });

//提交请假理由
    $("#go2").click(function () {
        let reasn = $("#reason").val();
        let meetingId = $("#meetingId").attr("value");
        var form4 = $("#form4");
        form4.validator({
            submit: function () {
                var formValidity = this.isFormValid();
                if (formValidity){
                    $.ajax({
                        url:testurl + "meeting/leave/",
                        type:"post",
                        headers: { "Authorization": "Bearer " + token},
                        contentType: "application/json",
                        data:JSON.stringify({
                            "meeting_id":meetingId,//会议id
                            "cause":reasn//事由
                        }),
                        success:function (data) {
                            if (data.success === true) {
                                $(".users").html("申请请假");
                                $(".alert").html(data.message);
                                $('#my-alert').modal({target: '#my-alert'});
                                $('#leave').modal('close');
                            }else {
                                $(".users").html("申请请假");
                                $(".alert").html(data.message);
                                $('#my-alert').modal({target: '#my-alert'});
                                $('#leave').modal('close');
                            }
                        }
                    });
                    return false
                } else {
                    return false
                }
            }
        });
    });

    //根据类型筛选
    let type ="";
    $("#choose").on("change",function () {
        type =  $("option:selected",this).val();
        loadData(1);
    });
    //页数
    var pages =1;
    function loadData(pageNum){
        $.ajax({
            url:testurl + "meeting/join_list",
            type:"post",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data:JSON.stringify({
                "pageNum": pageNum,
                "type":type
            }),
            success: function (data) {
                let yianmessage = data.obj;
                ssd = Math.ceil(data.pageTotal/data.pageSize);
                pages = ssd;
                str = "";
                $.each(yianmessage ,function (index,values) {
                    str += " <tr class=\"gradeX\">\n" +
                        "                                        <td>"+(index+1)+"</td>\n" +
                        "                                        <td>"+values.name+"</td>\n" +
                        "                                        <td>"+values.start_times+' -- '+values.end_times+"</td>\n" +
                        "                                        <td>"+values.address+"</td>\n" +
                        "                                        <td>"+values.starter_name+"</td>\n" +
                        "                                        <td>"+values.type_name+"</td>\n" +
                        "                                        <td>"+values.status_name+"</td>\n" +
                        "                                        <td>\n" +
                        "                                            <div class=\"tpl-table-black-operation\">\n" +
                        "                                                <a onclick=\"getMeetingDetail('"+values.id+"')\" data-am-modal=\"{target:'#metetingDetail'}\"  href=\"#\" class=\"tpl-table-black-operation\">\n" +
                        "                                                    <i class=\"am-icon-check\"></i> 查看\n" +
                        "                                                </a>\n";
                                                if (values.start_time < new Date().getTime() && values.leave_flag === 0){
                                                    str +="<a onclick=\"leave('"+values.name+"','"+values.start_times+"','"+values.id+"')\" data-am-modal=\"{target:'#leave'}\" href=\"#\" class=\"tpl-table-black-operation-del\"><i class=\"am-icon-check-square-o\"></i> 请假</a>\n"
                                                }
                  str+= "                                            </div>\n" +
                        "                                        </td>\n" +
                        "                                    </tr>";
                });
                $("#show").html(str);
            }
        });
    }



    loadData(1);
    //分页
    var pageNum = 1;
    $(".first").click(function(){
        pageNum =1;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".last").click(function(){
        pageNum = pages;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".up").click(function(){
        if(pageNum === 1){
            return false;
        } else{
            pageNum--;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".down").click(function(){
        if(pageNum === pages){
            return false;
        } else{
            pageNum++;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });

    // ztree
    let setting = {
        check: {
            enable: true
        },
        callback: {
            onNodeCreated: zTreeOnNodeCreated,
            onCheck: zTreeOnCheck

        }
    };
    $.ajax({
        url:testurl + "deputy/team/list",
        headers: { "Authorization": "Bearer " + token},
        type: "post",
        contentType: "application/json",
        data:JSON.stringify({
            "type": "1",
            "pageNum": "1"
        }),
        success:function (data) {
            zNodes = data.obj;
            zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        }
    });
    $('#metetingDetail').on('closed.modal.amui', function(){
       $("#qrcode").html('');
    });
        });
//请假查询
function leave(e,f,g) {
    $(".meetingName").html(e);
    $(".meetingTime").html(f);
    $("#meetingId").val(g);
}
let zNodes = [];
let newNodes = [];
//树节点创建
function zTreeOnNodeCreated(event, treeId, treeNode) {
    //
    // console.log(treeNode.id);
    if (treeNode.pid === undefined){
        $.ajax({
            url:testurl + "deputy/team/users",
            headers: { "Authorization": "Bearer " + token},
            type: "post",
            contentType: "application/json",
            data:JSON.stringify({
                "id":treeNode.id
            }),
            success:function (data) {
                newNodes = JSON.parse(JSON.stringify(data.obj));
                // console.log(newNodes)
                zTreeObj.addNodes(treeNode, newNodes,true);
            }
        });
    }
}
//树节点选中
let joinName = "";
let joinId = "";
function zTreeOnCheck(event, treeId, treeNode) {
    if (treeNode.pid === undefined){
        $.ajax({

            url:testurl + "deputy/team/users",
            headers: { "Authorization": "Bearer " + token},
            type: "post",
            contentType: "application/json",
            data:JSON.stringify({
                "id":treeNode.id
            }),
            success:function (data) {
                $.each(data.obj,function (index,values) {
                    joinName += values.name+',';
                    joinId +=values.id+',';
                })
            }
        });
    } else {
        joinName += treeNode.name+',';
        joinId +=values.id+',';
    }

}
$("#go3").click(function () {
            if (joinName === ""){
                alert("请选择！")
            } else {
                $("#jointlyers").val(joinName);
                $("#joinId").val(joinId);
                $('#daibiao').modal('close');
            }
});
//获取会议详情

let page2 = 1;
function getMeetingDetail(e,pageNum2) {
    let str = "";
    $.ajax({
        url:testurl + "meeting/detail",
        type:"post",
        headers: { "Authorization": "Bearer " + token},
        contentType: "application/json",
        data:JSON.stringify({
            "id": e
        }),
        success:function (data) {
            $("#mName").val(data.obj.name);
            $("#starterName").val(data.obj.starter_name);
            $("#attendNum").val(data.obj.attend_num);
            $("#leaveNum").val(data.obj.leave_num);
            $("#startTimes").val(data.obj.start_times);
            $("#endTimes").val(data.obj.end_times);
            $("#didian").val(data.obj.address);
            $("#record").val(data.obj.taker);
            $("#statusName").val(data.obj.status_name);
            $("#beizhu").val(data.obj.remark);
            new QRCode(document.getElementById('qrcode'),e);
            // if ($("#metetingDetail").style.display === "none"){
            //     alert(11)
            // }
        }
    });


    $.ajax({
        url:testurl + "meeting/leave/list_p",
        type:"post",
        headers: { "Authorization": "Bearer " + token},
        contentType: "application/json",
        data:JSON.stringify({
            "pageNum":pageNum2,
            "meeting_id":e
        }),
        success:function (data) {
             aab = Math.ceil(data.pageTotal/data.pageSize);
            page2 = aab;
            $.each(data.obj,function (index,values) {
                str += "<tr class=\"gradeX\">\n" +
                    "        <td>"+values.realname+"</td>\n" +
                    "        <td>"+values.cause+"</td>\n" +
                    "    </tr>";
            });
            $("#show2").html(str);
        }
    });

    $(".first1").click(function(){
        pageNum2 =1;
        getMeetingDetail(e,pageNum2);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".last").click(function(){
        pageNum2 = page2;
        getMeetingDetail(e,pageNum2);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".up").click(function(){
        if(pageNum2 === 1){
            return false;
        } else{
            pageNum2--;
            getMeetingDetail(e,pageNum2);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".down").click(function(){
        if(pageNum2 === page2){
            return false;
        } else{
            pageNum2++;
            getMeetingDetail(e,pageNum2);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
}
